package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenuDao menuDao;
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        Integer level = menuDao.findlevelByQueryString(queryString);
        Page<Menu> page = PageHelper.startPage(currentPage, pageSize);
        List<Menu> level1List = new ArrayList<>();
        //Page<Menu> page = menuDao.findPage(queryString);
        /*List<Menu> level1List = menuDao.findAllLevel1();
        for (Menu level1 : level1List) {
            List<Menu> level2List = menuDao.findAllLevel2(level1.getId());
            level1.setChildren(level2List);
        }
        System.out.println(page);*/

        if (!(queryString!=null && queryString.length()>0)){
            level1List = menuDao.findAllLevel1();
            for (Menu level1 : level1List) {
                List<Menu> level2List = menuDao.findAllLevel2(level1.getId());
                level1.setChildren(level2List);
            }
        }else {
            if (level==1){
                level1List= menuDao.findPage(queryString);
            }else {
                List<Menu> level2List = menuDao.findPage(queryString);
                Menu menu = level2List.get(0);
                Menu menu1 = menuDao.findById(menu.getParentMenuId());
                menu1.setChildren(level2List);
                level1List.add(menu1);
            }
        }
        System.out.println(page);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Menu> findAllLevel1() {
        return menuDao.findAllLevel1();
    }

    @Override
    public void add(Menu menu) {
        String path = getPath(menu);
        menu.setPath(path);
        menuDao.add(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu) {
        String path = getPath(menu);
        menu.setPath(path);
        menuDao.edit(menu);
    }

    @Override
    public void delete(Integer id) {
        //查询当前检查项是否和检查组有关联
        Integer level = menuDao.findLevelById(id);

        Long count = menuDao.findCountByMenuId(id);
        if (level==1||count>0){
            //当前检查项有和检查组引用，无法删除
            throw new RuntimeException("当前菜单被引用，不能删除");
        }
        //删除检查项
        menuDao.deleteById(id);
    }

    @Override
    public List<Menu> findByUsername(String username) {

        List<Menu> list = menuDao.findByUsername(username);

//        System.out.println(list);

        return list;
    }

    public String getPath(Menu menu){
        String parentPath = menuDao.findPath(menu.getParentMenuId());
        Integer parentId = menuDao.findParentId(menu.getId());
        Long count = menuDao.countlevel2(menu.getParentMenuId());
        String path = null;
        if (parentId!=menu.getParentMenuId()) {
            path = "/" + parentPath + "-" + (count + 1);
        }else {
            path = "/" + parentPath + "-" + count;
        }
        return path;
    }
}
